<?php
    
	/* 
		This is an example class script proceeding secured API
		To use this class you should keep same as query string and function name
		Ex: If the query string value rquest=delete_user Access modifiers doesn't matter but function should be
		     function delete_user(){
				 You code goes here
			 }
		Class will execute the function dynamically;
		
		usage :
		
		    $object->response(output_data, status_code);
			$object->_request	- to get santinized input 	
			
			output_data : JSON (I am using)
			status_code : Send status message for headers
			
		Add This extension for localhost checking :
			Chrome Extension : Advanced REST client Application
			URL : https://chrome.google.com/webstore/detail/hgmloofddffdnphfgcellkdfbfbjeloo
	 
	 	RESPONSE:
	  
	 		$this->response('', 200);	// If success everythig is good send header as "OK" and json back
	 		$this->response('', 204);	// If no records "No Content" status
	 		$this->response('', 404);	// If invalid inputs "Bad Request" status message and reason
 	*/
	
	require_once("Rest.inc.php");
	require_once("config.inc.php");
	
	error_reporting(E_ALL | E_STRICT);
	
	class API extends REST {
	
		public $data = "";
		
		const DB_SERVER = "localhost";
		const DB_USER = "root";
		const DB_PASSWORD = "odfred12";
		const DB = "appdata";
		
		private $db = NULL;
		private $dbpdo = NULL;
		private $initialresponse = array("api" => "v1.0", "success" => 0, "error" => 0);
	
		public function __construct(){
			parent::__construct();				// Init parent contructor
			$this->dbConnect();					// Initiate Database connection
		}
		
		/*
		 *  Database connection 
		*/
		private function dbConnect()
		{
		  /*
			try
			{
			    $this->db = new PDO('mysql:host=localhost;dbname='.self::DB, self::DB_USER, self::DB_PASSWORD);
			}
			catch (PDOException $e)
			{
			    print "Error!: " . $e->getMessage() . "<br/>";
			    die();
			}
       */
			
			//$this->db = mysql_connect(self::DB_SERVER,self::DB_USER,self::DB_PASSWORD);
			//if($this->db)
				//mysql_select_db(self::DB,$this->db);
		}
		
		/*
		 * Public method for access api.
		 * This method dynmically call the method based on the query string
		 *
		 */
		public function processApi()
		{
			if(isset($_REQUEST['rquest']))
			{
				$func = strtolower(trim(str_replace("/","",$_REQUEST['rquest'])));
				if((int)method_exists($this,$func) > 0)
					$this->$func();
				else
					$this->response('',404);				// If the method not exist with in this class, response would be "Page not found".
			}
			else
			{
				echo "Invalid API Call - check you're request.";
			}
		}

		/* 
		 *	Dummy Test Function
		 */
		private function credit_details()
		{
			if($this->get_request_method() != "GET")
			{
				$this->response('',406);
			}
		 	
		 	//$response = array(
		 		//"api" => "v1.0",
		 		//"status" => "Success",
		 		//"msg" => "Moo means milk."
			//);
			//$this->response($this->json($response), 200);
			
			$thing = '
{ "Products" : [ { "apsCardID" : "44445555666677778888",
        "lastPaymentAmount" : "110.00",
        "lastPaymentDate" : "2013-04-29",
        "nextPaymentAmount" : "155.00",
        "nextPaymentDate" : "2013-05-17",
        "product" : "iAdvance"
      },
      { "apsCardID" : "44445555666677778888",
        "lastPaymentAmount" : "200.00",
        "lastPaymentDate" : "2013-05-01",
        "nextPaymentAmount" : "",
        "nextPaymentDate" : "",
        "product" : "debitProtect"
      }
    ] }			
			';
			
			$this->response($thing, 200);
			
		}
		 
		/*
		 *	Encode array into JSON
		*/
		private function json($data){
			if(is_array($data)){
				return json_encode($data);
			}
		}
	}
	
	// Initiiate Library
	
	$api = new API;
	$api->processApi();
?>